﻿//infostate.js
var mapTools=
{
    isSendMistake:false,isSendMail:false,formatLen:function(str,len)
    {
        var z=1;
        if(str.charCodeAt(0)>255||str.charCodeAt(str.length-1)>255) z=2;
        if(str.length*z<=len) return str;
        else return str.substring(0,Math.floor(len/z))+'...';
    }
    ,copyValue:function(id)
    {
        var obj=$(id);
        if(!obj) return;
        window.clipboardData.setData("Text",obj.value);
        national.showMsg("% {Copy2Clipboard:已经复制到剪贴板}!");
    }
    ,validateMistakeForm:function(form)
    {
        if (this.isSendMistake)
        {
            national.showMsg('%{noSubmitAgain:请不要重复提交}');
            return false;
        }
        var emptyRe=/^[\s ]*$/;
        if(emptyRe.test(form.content.value))
        {
            national.showMsg('%{EmptyContent: 内容不能为空}');
            return false;
        }
        form.centerX.value=mapApi.AreaUnitInfo.x;
        form.centerY.value=mapApi.AreaUnitInfo.y;
        form.entity.value=mapApi.AreaUnitInfo.AreaID;
        form.map.value=mapApi.mapid;
        if(mapApi.type==0)
        {
            this.isSendMistake=true;
            mapApi.setView_comInfoSend(true);
            setTimeout(this.errorMistake.bind(this),5000);
        }
        else
        {
            mapApi.switchComPanel (0);
            national.showMsg('%{InfoOK:您的信息已经提交}');
        }
        return true;
    }
    ,backMistake:function()
    {
        this.isSendMistake=false;
        mapApi.mistakeBack();
    }
    ,errorMistake:function()
    {
        if(!this.isSendMistake) return false;
        this.isSendMistake=false;
        this.changeSendView();
        national.showMsg('%{InfoTimeout: 超时，信息可能发送失败}！');
    }
    ,validateMailForm:function(form)
    {
        if(this.isSendMail)
        {
            national.showMsg('%{noSubmitAgain:请不要重复提交}');
            return false;
        }
        form=document.forms[form];
        var emailRe=/^\w+@\w+\.\w+$/;
        var emptyRe=/^[\s ]*$/;
        var toAddress=form.toAddress.value;
        if(!emailRe.test(toAddress))
        {
            national.showMsg ('%{ErrorEmail:接收邮件地址为空或者错误}');
            return false;
        }
        var replyAddress=form.replyAddress.value;
        if(!emptyRe.test(replyAddress)&&! emailRe.test(replyAddress))
        {
            national.showMsg('%{ErrorYourEmail:您自己的邮件地址错误}');
            return false;
        }
        var myname=form.myname.value;
        if(emptyRe.test(myname))
        {
            national.showMsg('%{EmptyName: 名字为空}');
            return false;
        }
        var mycontent=form.mycontent.value;
        if(emptyRe.test(mycontent))
        {
            national.showMsg ('%{EmptyContent:内容不能为空}');
            return false;
        }
        var url=null;
        if(mapApi.AreaUnitInfo.UnitID) url=mapApi.host+'/?cid='+mapApi.AreaUnitInfo.UnitID;
        else url=mapApi.host+'/?id='+mapApi.AreaUnitInfo.AreaID;
        form.content.value=national.formatStr ('MailContent',
        {
            YourName:myname,YourEmail:replyAddress,Url:url,Content:mycontent
        }
        );
        if(mapApi.type==0)
        {
            this.isSendMail=true;
            mapApi.setView_comInfoSend(true);
            setTimeout(this.errorMail.bind(this),5000);
        }
        else
        {
            mapApi.switchComPanel(0);
            national.showMsg('%{MailOK:您的邮件已经发送}');
        }
        form.submit();
        return true;
    }
    ,backMail:function()
    {
        this.isSendMail=false;
        mapApi.mailBack();
    }
    ,errorMail:function()
    {
        if(!this.isSendMail) return false;
        this.isSendMail=false;
        this.changeSendView();
        national.showMsg('%{MailTimeout: 超时，邮件可能发送失败}！');
    }
    ,changeSendView:function(flag)
    {
        mapApi.switchComPanel (0);
        mapApi.setView_comInfoSend(flag);;
    }
};